package cn.bluech.example.ui.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;


import cn.bluech.example.R;
import cn.bluech.lib.ui.widget.SlidingUpPanelLayout;
import cn.bluech.lib.utils.DispUtils;

import static cn.bluech.lib.ui.widget.SlidingUpPanelLayout.COLLAPSED;


/**
 * Description:
 * Create by Codeant on 2019/12/16.
 */
public class WeatherPanelView extends FrameLayout implements SlidingUpPanelLayout.ISlidingUpPanel<View> {
    protected static int MAX_RADIUS;

    protected int mExpendedHeight;
    protected int mFloor; // 由下至上第几层，最高三层
    protected int mPanelHeight;
    protected int mRealPanelHeight;
    protected int mSlideState = COLLAPSED;
    protected float mSlope; // 斜率

    GradientDrawable colorDrawable = new GradientDrawable();

    public WeatherPanelView(@NonNull Context context) {
        this(context,null);
    }

    public WeatherPanelView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public WeatherPanelView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        MAX_RADIUS = DispUtils.dp2px(context,16);
        ViewCompat.setElevation(this, DispUtils.dp2px(context,16));

        View.inflate(context,R.layout.view_panel_weather, this);

        colorDrawable.setColor(Color.TRANSPARENT);
        colorDrawable.setCornerRadius(MAX_RADIUS);
        setBackgroundDrawable(colorDrawable);
    }

    @Override
    public void setBackgroundColor(int color) {
        colorDrawable.setColor(color);
    }

    @NonNull
    @Override
    public View getPanelView() {
        return this;
    }


    public void setFloor(int floor) {
        mFloor = floor;

        mRealPanelHeight = 0;
    }

    public int getFloor() {
        return mFloor;
    }

    public void setPanelHeight(int panelHeight) {
        mPanelHeight = panelHeight;
    }

    public int getRealPanelHeight() {
        if (mRealPanelHeight == 0)
            mRealPanelHeight = mFloor * mPanelHeight;

        return mRealPanelHeight;
    }

    private void checkVisibilityOfViews() {

        if (mSlideState == COLLAPSED) {
            colorDrawable.setCornerRadius(MAX_RADIUS);

//            mMenuLayout.setAlpha(0f);
//            mExpendLayout.setAlpha(0f);
//            mCollapseLayout.setAlpha(1f);
        } else if (mSlideState == SlidingUpPanelLayout.EXPANDED) {
            colorDrawable.setCornerRadius(0);

//            mMenuLayout.setAlpha(1f);
//            mExpendLayout.setAlpha(1f);
//            mCollapseLayout.setAlpha(0f);
        }
    }


    @Override
    public int getPanelExpandedHeight() {
        if (mExpendedHeight == 0) {
            DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
            if (Build.VERSION.SDK_INT > 16) {
                WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
                if (windowManager != null) {
                    windowManager.getDefaultDisplay().getRealMetrics(dm);
                }
            }
            mExpendedHeight = dm.heightPixels;
        }
        return mExpendedHeight;
    }

    @Override
    public int getPanelCollapsedHeight() {
        return getRealPanelHeight();
    }

    @Override
    public int getSlideState() {
        return mSlideState;
    }

    @Override
    public int getPanelTopBySlidingState(@SlidingUpPanelLayout.SlideState int slideState) {
        if (slideState == SlidingUpPanelLayout.EXPANDED) {
            return 0;
        } else if (slideState == COLLAPSED) {
            return getPanelExpandedHeight() - getPanelCollapsedHeight();
        } else if (slideState == SlidingUpPanelLayout.HIDDEN) {
            return getPanelExpandedHeight();
        }
        return 0;
    }

    @Override
    public void setSlideState(@SlidingUpPanelLayout.SlideState int slideState) {
        mSlideState = slideState;

        if (mSlideState != SlidingUpPanelLayout.EXPANDED) {
            mSlope = 0;
        }

        checkVisibilityOfViews();
    }

    @Override
    public void onSliding(@NonNull SlidingUpPanelLayout.ISlidingUpPanel<? extends View> panel, int top, int dy, float slidedProgress) {
        if (panel != this) {
            int myTop = (int) (getPanelExpandedHeight() + getSlope(((WeatherPanelView) panel).getRealPanelHeight()) * top);
            setTop(myTop);
        }

        if (dy < 0) { // 向上
            if (MAX_RADIUS >= top) {
                colorDrawable.setCornerRadius(top);
            }

//            float alpha = mCollapseLayout.getAlpha();
//            if (alpha > 0f && top < 200) {
//                alpha += dy / 200.0f;
//                mCollapseLayout.setAlpha(alpha < 0 ? 0 : alpha); // 逐隐
//            }
//
//            alpha = mMenuLayout.getAlpha();
//            if (alpha < 1f && top < 100) {
//                alpha -= dy / 100.0f;
//                mMenuLayout.setAlpha(alpha > 1 ? 1 : alpha); // 逐显
//            }
//
//            alpha = mExpendLayout.getAlpha();
//            if (alpha < 1f) {
//                alpha -= dy / 1000.0f;
//                mExpendLayout.setAlpha(alpha > 1 ? 1 : alpha); // 逐显
//            }
        } else { // 向下
            if (top < MAX_RADIUS) {
                colorDrawable.setCornerRadius(top);
            }

//            float alpha = mCollapseLayout.getAlpha();
//            if (alpha < 1f) {
//                alpha += dy / 800.0f;
//                mCollapseLayout.setAlpha(alpha > 1 ? 1 : alpha); // 逐显
//            }
//
//            alpha = mMenuLayout.getAlpha();
//            if (alpha > 0f) {
//                alpha -= dy / 100.0f;
//                mMenuLayout.setAlpha(alpha < 0 ? 0 : alpha); // 逐隐
//            }
//
//            alpha = mExpendLayout.getAlpha();
//            if (alpha > 0f) {
//                alpha -= dy / 1000.0f;
//                mExpendLayout.setAlpha(alpha < 0 ? 0 : alpha); // 逐隐
//            }
        }
    }

    public float getSlope(int slidingViewRealHeight) {
        if (mSlope == 0) {
            mSlope = -1.0f * getRealPanelHeight() / (getPanelExpandedHeight() - slidingViewRealHeight);
        }

        return mSlope;
    }
}
